import java.util.Scanner;

/**
 * A program that simulate the car dealership.
 *
 * <p>Purdue University -- CS18000 -- Summer 2020 -- PJ02</p>
 *
 * @author Wenru He
 * @version July 8, 2020
 */

public class CarDealership {
    private Car carOne;
    private Car carTwo;
    private Car carThree;
    private double dailyTransactions = 0;
    private int panswer = 0;
    private int sanswer = 0;
    private int sanswer2 = 0;
    Scanner scanner = new Scanner(System.in);

    public CarDealership(Car carOne, Car carTwo, Car carThree) {
        this.carOne = carOne;
        this.carTwo = carTwo;
        this.carThree = carThree;

    }

    public CarDealership() {
        this.carOne = new Car("unkonw", "unkonw", 0);
        this.carTwo = new Car("unkonw", "unkonw", 0);
        this.carThree = new Car("unkonw", "unkonw", 0);
    }

    public void performMaintenance(Scanner scan) {
        System.out.println("Welcome to the Maintenance Menu!");
        String next = null;
        do {
            System.out.println("Please select the service you wish to record:");
            System.out.println("1. Oil Change ($50)");
            System.out.println("2. Tire Rotation ($30)");
            System.out.println("3. Detailing ($100)");
            System.out.println("4. Tune-up ($200)");
            String input = scan.nextLine();
            switch (input) {
                case "[1]":
                    this.dailyTransactions += 50;
                    break;
                case "[2]":
                    this.dailyTransactions += 30;
                    break;
                case "[3]":
                    this.dailyTransactions += 100;
                    break;
                case "[4]":
                    this.dailyTransactions += 200;
                    break;
                default:
                    System.out.println("An error occurred!");
                    return;
            }
            System.out.println("Your selection has been recorded!" +
                    "\nDid you sell another service?" +
                    "\n1. Yes\n" +
                    "2. No");
            next = scan.nextLine();
        } while (next.equals("[1]"));

        if (next.equals("[2]")) {
            System.out.println("Thank you! Now returning to the Main Menu...");
        } else {
            System.out.println("An error occurred!");
        }
    }

    public void sellCar(Scanner scan) {
        System.out.println("Welcome to the Sales Menu!");
        while (true) {
            if (carOne.getYear() == 0 && carTwo.getYear() == 0 && carThree.getYear() == 0) {
                System.out.println("No vehicles currently available. ");
                break;
            }

            printInventory();
            System.out.println("Did you sell PrintLinkList vehicle?");
            System.out.println("1. Yes");
            System.out.println("2. No ");
            String input = scan.nextLine();
            if (input.equals("[1]")) {
                System.out.println("Which vehicle was sold? " +
                        "Please enter the Make, Model, and Year separated by commas. ");
                String input1 = scan.nextLine();
                boolean ret = false;
                if (input1.equals(carOne.lineInputLike())) {
                    carOne = new Car(" ", " ", 0);
                    ret = true;
                } else if (input1.equals(carTwo.lineInputLike())) {
                    carTwo = new Car(" ", " ", 0);
                    ret = true;
                } else if (input1.equals(carThree.lineInputLike())) {
                    carThree = new Car(" ", " ", 0);
                    ret = true;
                }
                if (ret) {
                    System.out.println("The vehicle common.list has been updated!");
                    dailyTransactions -= 15000;
                } else {
                    System.out.println("An error occurred!");
                    return;
                }
            } else if (input.equals("[2]")) {
                break;
            } else {
                System.out.println("An error occurred!");
                break;
            }
        }
        System.out.println("Thank you! Now returning to the Main Menu...");

    }

    public void buyCar(Scanner scan) {
        System.out.println("Welcome to the  Purchasing Menu!");
        while (true) {
            printInventory();
            if (carOne.getYear() != 0 && carThree.getYear() != 0 && carTwo.getYear() != 0) {
                System.out.println("The test.Vehicle Inventory is now full.");
                break;
            }

            System.out.println("Did you purchase PrintLinkList vehicle?");
            System.out.println("1. Yes");
            System.out.println("2. No");
            String input = scan.nextLine();
            if (input.equals("[1]")) {
                System.out.println("Please enter the test.Vehicle Details! \n" + "Make:");
                String input1 = scan.nextLine();
                System.out.println("Model:");
                String input2 = scan.nextLine();
                System.out.println("Year:");
                int input3 = Integer.parseInt(scan.nextLine());
                if (carOne.getYear() == 0) {
                    carOne.setMake(input1);
                    carOne.setModel(input2);
                    carOne.setYear(input3);
                } else if (carTwo.getYear() == 0) {
                    carTwo.setMake(input1);
                    carTwo.setModel(input2);
                    carTwo.setYear(input3);
                } else {
                    carThree.setMake(input1);
                    carThree.setModel(input2);
                    carThree.setYear(input3);
                }
            } else if (input.equals("[2]")) {
                break;
            } else {
                System.out.println("An error occurred!");
            }

        }
        System.out.println("Thank you! Now returning to the Main Menu...");
    }

    public void printInventory() {
        if (carOne.getYear() != 0 || carThree.getYear() != 0 || carTwo.getYear() != 0) {
            System.out.println("Current Inventory is Listed Below:");
            if (carOne.getYear() != 0) {
                System.out.println(carOne.toString());
            }
            if (carTwo.getYear() != 0) {
                System.out.println(carTwo.toString());
            }
            if (carThree.getYear() != 0) {
                System.out.println(carThree.toString());
            }
        }
    }

    public void menu() {
        while (true) {
            System.out.println("\nWelcome to the Main Menu!");
            System.out.printf("The Daily Transactions value is currently: $%.2f\n", dailyTransactions);
            System.out.println("Please select an Option");
            System.out.println("1. Perform Maintenance");
            System.out.println("2. Sell Car");
            System.out.println("3. Buy Car");
            System.out.println("4. Print Inventory");
            System.out.println("5. Quit");

            String input = scanner.nextLine();
            switch (input) {
                case "[1]":
                    performMaintenance(scanner);
                    System.out.println("Thank you for using the Car Dealership program!");
                    break;
                case "[2]":
                    sellCar(scanner);
                    System.out.println("Thank you for using the Car Dealership program!");
                    break;
                case "[3]":
                    buyCar(scanner);
                    System.out.println("Thank you for using the Car Dealership program!");
                    break;
                case "[4]":
                    printInventory();
                    System.out.println("Thank you for using the Car Dealership program!");
                    break;
                case "[5]":
                    System.out.println("Thank you for using the Car Dealership program!");
                    return;
                default:
                    System.out.println("An error occurred!");
                    break;
            }
        }
    }

    public static void main(String[] args) {
        CarDealership carDealership = new CarDealership();
        carDealership.menu();
    }

}
